//
//  XPEncryptUtil.swift
//  PPKit
//
//  Created by xiaopin on 2020/9/12.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//
/*
   通用加密解密工具调度类：包含当前最高级最好的用法，如：AES(128,192,256),Blowfish, RSA等，
   参考：https://github.com/krzyzanowskim/CryptoSwift
        https://www.cnblogs.com/Saoz/p/9996645.html
 */
import CryptoSwift


/// 加密解密
public class XPEncryptUtil {
    static let key = "Dds#Safu3!4R5S*2"
    static let iv = "1234567890abcd"

    static func AESEncrypt(_ str : String) -> String {
        var lastStr = ""
        do {
            let aes = try AES(key: key.bytes, blockMode: ECB())
            let encrypted = try aes.encrypt(str.bytes)
            lastStr = encrypted.toBase64()
        } catch { }
        return lastStr
    }
    
    static func AESDecrypt(_ encryptStr : String) -> String {
        var lastStr = ""
        do {
            let aes = try AES(key: key.bytes, blockMode: ECB())
            lastStr = try encryptStr.decryptBase64ToString(cipher: aes)
        } catch { }
        return lastStr
    }
    
    /// Blowfish加密
    /// - Parameter str: 原文
    /// - Returns: 密文
    static func BlowfishEncrypt(_ str:String) -> String{
        var lastStr = ""
        do {
            let chiper = try Blowfish(key: key.bytes, blockMode: ECB(), padding: .pkcs7)
            let encrypted = try chiper.encrypt(str.bytes)
            lastStr = encrypted.toBase64()
        } catch { }
        return lastStr
    }
    
    /// Blowfish解密
    /// - Parameter encryptStr: 密文
    /// - Returns: 原文
    static func BlowfishDecrypt(_ encryptStr:String) -> String{
        var lastStr = ""
        do {
            let chiper = try Blowfish(key: key.bytes, blockMode: ECB(), padding: .pkcs7)
            lastStr = try encryptStr.decryptBase64ToString(cipher: chiper)
        } catch { }
        return lastStr
    }
}

